import fs from "fs";

class Formatter {
  static convertToMarkdown(questions, options = { column: 4 }) {
    const writer = [];
    writer.push("|" + " | ".repeat(options.column - 1) + "|");
    writer.push("|" + " -------- |".repeat(options.column));

    let currentQuestions = questions.splice(0, options.column);
    while (currentQuestions.length > 0) {
      writer.push(
        "| " +
          currentQuestions.map((s) => s + "&nbsp;".repeat(10)).join(` | `) +
          " |",
      );
      writer.push("|" + " | ".repeat(options.column - 1) + "|");
      currentQuestions = questions.splice(0, options.column);
    }

    return writer.join("\n");
  }

  static convertToHtml(questions, options = { column: 4 }) {
    const writer = [];
    let currentQuestions = questions.splice(0, options.column);
    while (currentQuestions.length > 0) {
      writer.push("<tr>");
      writer.push(currentQuestions.map((s) => `<td>${s}</td>`).join(""));
      writer.push("</tr>");
      currentQuestions = questions.splice(0, options.column);
    }
    const questionBody = writer.join("\n");

    const template = fs.readFileSync("./templates/math.html");
    return template.toString().replace("{{questions}}", questionBody);
  }
}

export default Formatter;
